package com.mrobuy.jdvc.dao.impl;

import com.mrobuy.jdvc.dao.HtmlCacheDao;
import com.mrobuy.jdvc.dao.TaskDao;
import com.mrobuy.jdvc.entity.HtmlCache;
import com.mrobuy.jdvc.entity.Task;
import com.mrobuy.util.MD5Utils;
import io.micrometer.core.instrument.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository("htmlCacheDao")
public class HtmlCacheDaoImpl implements HtmlCacheDao {

    private static Logger LOG = LoggerFactory.getLogger(HtmlCacheDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public HtmlCache load(String url) {

        if ( StringUtils.isBlank(url) ) {
            return null;
        }

        String id = MD5Utils.md5Hex(url);
        String suffix =  id.substring(id.length()-1);

        System.out.println( "[" + suffix + "][" + id + "]" + url);

        try {
            HtmlCache htmlCache = (HtmlCache)jdbcTemplate.queryForObject("select * from htmlcache_" + suffix + "  where ID = ? ", new Object[]{id}, new BeanPropertyRowMapper(HtmlCache.class));
            return htmlCache;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return null;

    }
}
